package com.bluecat.utils;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import com.bluecat.entity.ColumnEntity;
import com.bluecat.entity.TableEntity;
 
public class GenUtils {
	/** 时间格式(yyyy-MM-dd) */
	public final static String DATE_PATTERN = "yyyy-MM-dd";
	/** 时间格式(yyyy-MM-dd HH:mm:ss) */
	public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
 
	public static String format(Date date) {
		return format(date, DATE_PATTERN);
	}
 
	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}
 
	public static List<String> getTemplates() {
		List<String> templates = new ArrayList<>();
		templates.add("templates/entity.java.vm");
		templates.add("templates/service.java.vm");
		templates.add("templates/serviceImpl.java.vm");
		templates.add("templates/mapper.java.vm");
		templates.add("templates/fallback.java.vm");
		templates.add("templates/client.java.vm");
		templates.add("templates/controller.java.vm");
		templates.add("templates/mapper.xml.vm");
		return templates;
	}
 
	/**
	 * 生成代码
	 */
	public static void generatorCode(Map<String, Object> table, List<Map<String, Object>> columns, ZipOutputStream zip) {
		// 表信息
		TableEntity tableEntity = new TableEntity();
		tableEntity.setTableName(table.get("tableName") + "");
		tableEntity.setComments(table.get("tableComment") + "");
		// 表名转换成Java类名
		String className = tableToJava(tableEntity.getTableName(), CommonMap.javaTypeMap.get("tablePrefix"));
		tableEntity.setClassName(className);
		tableEntity.setClassname(StringUtils.uncapitalize(className));
		// 列信息
		List<ColumnEntity> columsList = new ArrayList<>();
		for (Map<String, Object> column : columns) {
			ColumnEntity columnEntity = new ColumnEntity();
			columnEntity.setColumnName(column.get("columnName") + "");
			columnEntity.setDataType(column.get("dataType") + "");
			columnEntity.setComments(column.get("columnComment") + "");
			
			columnEntity.setColumnKey(column.get("columnKey").toString());
			columnEntity.setExtra(column.get("extra") + "");
 
			// 列名转换成Java属性名
			String attrName = columnToJava(columnEntity.getColumnName());
			columnEntity.setAttrName(attrName);
			columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
 
			// 列的数据类型，转换成Java类型
			String attrType = CommonMap.javaTypeMap.get(columnEntity.getDataType());
			attrType = StringUtils.isBlank(attrType) ? "unknowType" : attrType;
			columnEntity.setAttrType(attrType);
 
			// 是否主键
			if ("PRI".equalsIgnoreCase(column.get("columnKey") + "") && tableEntity.getPk() == null) {
				tableEntity.setPk(columnEntity);
			}
			columsList.add(columnEntity);
		}
		tableEntity.setColumns(columsList);
 
		// 没主键，则第一个字段为主键
		if (tableEntity.getPk() == null) {
			tableEntity.setPk(tableEntity.getColumns().get(0));
		}
 
		// 设置velocity资源加载器
		Properties prop = new Properties();
		prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		Velocity.init(prop);
 
		// 封装模板数据
		Map<String, Object> map = new HashMap<>();
		map.put("tableName", tableEntity.getTableName());
		map.put("comments", tableEntity.getComments());
		map.put("pk", tableEntity.getPk());
		map.put("className", tableEntity.getClassName());
		map.put("classname", tableEntity.getClassname());
		map.put("pathName", tableEntity.getClassname().toLowerCase());
		map.put("columns", tableEntity.getColumns());
		map.put("baseResultMap", true);
		map.put("baseColumnList", true);
		map.put("package", CommonMap.javaTypeMap.get("package"));
		map.put("author", CommonMap.javaTypeMap.get("author"));
		map.put("date", format(new Date(), DATE_PATTERN));
//		map.put("db", db);
		map.put("jdk", CommonMap.javaTypeMap.get("jdk"));
		map.put("version", CommonMap.javaTypeMap.get("version"));
		VelocityContext context = new VelocityContext(map);
 
		// 获取模板列表
		List<String> templates = getTemplates();
		for (String template : templates) {
			// 渲染模板
			StringWriter sw = new StringWriter();
			Template tpl = Velocity.getTemplate(template, "UTF-8");
			tpl.merge(context, sw);
 
			try {
				// 添加到zip
				zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), CommonMap.javaTypeMap.get("package"))));
				IOUtils.write(sw.toString(), zip, "UTF-8");
				IOUtils.closeQuietly(sw);
				zip.closeEntry();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	/**
	 * 列名转换成Java属性名
	 */
	public static String columnToJava(String columnName) {
		return WordUtils.capitalizeFully(columnName, new char[] { '_' }).replace("_", "");
	}
 
	/**
	 * 表名转换成Java类名
	 */
	public static String tableToJava(String tableName, String tablePrefix) {
		if (StringUtils.isNotBlank(tablePrefix)) {
			tableName = tableName.replace(tablePrefix, "");
		}
		return columnToJava(tableName);
	}
 
	/**
	 * 获取文件名
	 */
	public static String getFileName(String template, String className, String packageName) {
		String packagePath = "";
		if (StringUtils.isNotBlank(packageName)) {
			packagePath += packageName.replace(".", File.separator) + File.separator;
		}
		if (template.contains("entity.java.vm")) {
			return packagePath + "model" + File.separator+""+File.separator+ className + ".java";
		}
		if (template.contains("service.java.vm")) {
			return packagePath + "service" + File.separator + className + "Service.java";
		}
		if (template.contains("serviceImpl.java.vm")) {
			return packagePath + "service" + File.separator + "impl" + File.separator + className + "ServiceImpl.java";
		}
		if (template.contains("mapper.java.vm")) {
			return packagePath + "mapper" + File.separator + className + "Mapper.java";
		}
		if (template.contains("client.java.vm")) {
			return packagePath + "client" + File.separator + className + "Client.java";
		}
		if (template.contains("fallback.java.vm")) {
			return packagePath + "fallback" + File.separator + className + "Fallback.java";
		}
		if (template.contains("controller.java.vm")) {
			return packagePath + "controller" + File.separator + className + "Controller.java";
		}
		if (template.contains("mapper.xml.vm")) {
			return packagePath + "mapping" + File.separator + className + "Mapper.xml";
		}
		return null;
	}
 
}